<script>
import QSGDrawer from "@/components/QSGDrawer/index.vue";
import {addOrUpdateCostumes} from "@/api/aihuman/humanAssets/dress";
import {uploadFile} from "@/api/aihuman/common/minio";

export default {
  name: "AddUeDress",
  components: {QSGDrawer},
  data() {
    //检验声音id
    var checkType = (rule, value, callback) => {
      // var inputType = /[^\w]/g
      var inputReg = /[a-zA-Z0-9-_]/g;
      if (!inputReg.test(value)) {
        callback(new Error('id由数字、英文、短划线、下划线组成'))
      } else {
        callback()
      }
    };
    return {
      typesList: [
        {
          label: 'Web3D',
          value: 1
        },
        {
          label: 'UE3D',
          value: 2
        }
      ],
      formStatus: 'add',
      showDrawer: false,
      title: "新增",
      loading: false,
      groupId: '',
      form: {
        costumesName: '',
        costumesCode: '',
        costumesVideoLink: '',
        costumesTag: '',
        status: '',
        costumesFile: '',
        costumesType: ''
      },
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      rules: {
        costumesName: [
          {
            required: true,
            message: '请输入服饰名称',
            trigger: 'blur'
          },
          {min: 1, max: 30, message: '长度不可超过30个字符', trigger: 'blur'}
        ],
        costumesCode: [
          {required: true, message: '请输入服饰id', trigger: 'blur'},
          {min: 1, max: 50, message: '长度不可超过50个字符', trigger: 'blur'},
          {required: true, validator: checkType, trigger: 'blur'}
        ],
        costumesVideoLink: [
          {required: true, message: '请上传服饰图', trigger: 'change'},
        ],
        costumesFile: [
          {required: true, message: '请上传服饰文件', trigger: 'change'},
        ],
        costumesType: [
          {required: true, message: '请输入选择类型', trigger: 'change'},
        ],
      }
    }
  },
  methods: {
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    open(type, groupId, data = {}) {
      console.log(data);
      if (type === 'add') {
        this.showDrawer = true
        this.groupId = groupId;
        this.formStatus = type
        this.form = {
          costumesName: '',
          costumesCode: '',
          costumesVideoLink: '',
          costumesTag: '',
          status: '',
          costumesFile: '',
          costumesType: ''
        }
        this.$nextTick(() => {
          this.$refs.form.clearValidate()
        })
      } else if (type === 'preview') {
        this.title = '查看'
        this.formStatus = type
        this.showDrawer = true
        this.groupId = groupId;
        this.form = {...data}
        this.dynamicTags = data.costumesTag ? data.costumesTag.split(',') : []
      } else {
        this.title = '编辑'
        this.formStatus = type
        this.showDrawer = true
        this.groupId = groupId;
        this.form = {...data}
        this.dynamicTags = data.costumesTag ? data.costumesTag.split(',') : []
      }
    },
    async uploadVideo(file) {
      let formData = new FormData()
      formData.append('file', file.file)
      formData.append('keyFolderPath', `HumanAssets/dress/${this.groupId}`)

      const {data} = await uploadFile(formData)
      this.form.costumesVideoLink = data.data.filePath
      this.$refs.form.validateField('costumesVideoLink')
    },

    async uploadFile(file) {
      let formData = new FormData()
      formData.append('file', file.file)
      formData.append('keyFolderPath', `HumanAssets/dress/${this.groupId}`)

      const {data} = await uploadFile(formData)
      this.form.costumesFile = data.data.filePath
      this.$refs.form.validateField('costumesFile')
    },
    handleUpload(file) {

    },
    closeDawer() {
      this.showDrawer = false
    },
    submitData() {
      this.loading = true
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          let params = {
            ...this.form,
            costumesGroupId: this.groupId,
            costumesTag: this.dynamicTags.join(',')
          }

          try {
            const {data} = await addOrUpdateCostumes(params)
            console.log(data);
            this.loading = false
            this.showDrawer = false
            this.$emit('save')
          } catch (e) {
            console.log(e);
            this.loading = false
          }

        } else {
          console.log('error submit!!');
          this.loading = false
          return false;
        }
      });
    }
  }
}
</script>

<template>
  <q-s-g-drawer
    ref="drawer"
    :visible.sync="showDrawer"
    size="600px"
    :title="title"
  >

    <el-form ref="form" :model="form" :rules="rules" label-width="auto" size="small">
      <el-form-item label="服饰名称:" prop="costumesName">
        <el-input v-if="formStatus !== 'preview'" v-model="form.costumesName"></el-input>
        <div v-else>{{ form.costumesName }}</div>
      </el-form-item>

      <el-form-item label="服饰ID:" prop="costumesCode">
        <el-input v-if="formStatus !== 'preview'" v-model="form.costumesCode"></el-input>
        <div v-else>{{ form.costumesCode }}</div>
      </el-form-item>

      <el-form-item label="资源包路径:" prop="costumesCode">
        <el-input v-if="formStatus !== 'preview'" v-model="form.costumesCode"></el-input>
        <div v-else>{{ form.costumesCode }}</div>
      </el-form-item>

      <el-form-item label="服饰预览:" prop="costumesVideoLink">
        <el-upload
          v-if="formStatus !== 'preview'"
          class="upload-demo"
          :http-request="uploadVideo"
          :on-change="handleUpload"
          accept="image/jpeg"
          :show-file-list="false"
          drag>
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">只能上传jpg文件</div>
        </el-upload>

        <div v-if="form.costumesVideoLink">
          <img :src="`${window.location.origin}/` + form.costumesVideoLink" alt="" style="height: 100px">
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button
        v-if="formStatus !== 'preview'"
        type="primary"
        icon="el-icon-circle-plus-outline"
        size="small"
        :loading="loading"
        @click="submitData"
      >保 存
      </el-button>
      <el-button
        type="default"
        icon="el-icon-circle-close"
        size="small"
        :loading="loading"
        @click="closeDawer"
      >{{ formStatus === 'preview' ? '关 闭' : '取 消' }}
      </el-button>
    </template>
  </q-s-g-drawer>
</template>

<style scoped lang="scss">
.el-tag + .el-tag {
  margin-left: 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
